<?php
namespace app\index\controller;

use app\common\Base;
use app\index\model\Business;
use app\index\model\CommodityAppraises;
use app\index\model\Favorites;
use app\index\model\Order;
use app\index\model\History;
use think\Exception;
use think\Request;

class HistoryAndFavorites extends Base
{
    //历史店铺
    public function History(Request $request,History $history,Business $business,Order $order,CommodityAppraises $commodityAppraises){
        try{
            // TODO 将获取缓存信息放在中间件里
            //获取请求头
            $arr = $request->cacheInfo;
            //通过history表找出用户和商家Id
            if (($historyInfo = $history->findHistoryInfo($arr['id']))->isEmpty()) {
                return responseJson(Base::OK,'暂无历史店铺');
            }
            //将信息转为数组
            $arrHistoryInfo = json_decode($historyInfo,true);
            //for循环数据
            for ($i=0;$i<count($arrHistoryInfo);$i++){
                //通过businessId找到businessName,businessImg,businessRange
                $arrHistoryInfo_businessInfo = $business->findHistoryInfo($arrHistoryInfo[$i]['businessId']);
                //将数据转为数组
                $arrBusinessInfo = json_decode($arrHistoryInfo_businessInfo,true);
                //循环数据
                for ($j=0;$j<count($arrBusinessInfo);$j++){
                    $arrHistoryInfo[$i]['businessName'] = $arrBusinessInfo[$j]['businessName'];
                    $arrHistoryInfo[$i]['businessImg'] = $arrBusinessInfo[$j]['businessImg'];
                    $arrHistoryInfo[$i]['businessRange'] = $arrBusinessInfo[$j]['businessRange'];
                }

                //月销
                //获取月初年月日
                $beginDay = date('Y-m-01');
                //获取月末年月日
                $endDay = date('Y-m-d',strtotime("$beginDay +1 month -1 day"));
                //获取当前月份的销售额(月销值)
                $allOrderNum = $order->monthNum($beginDay,$endDay);

                //放入数组
                //五星评价
                $arrHistoryInfo[$i]['Appraises'] = ceil(($commodityAppraises->commodityScore($arrHistoryInfo[$i]['businessId']) + $commodityAppraises->serviceScore($arrHistoryInfo[$i]['businessId']) + $commodityAppraises->timeScore($arrHistoryInfo[$i]['businessId']))/3);
                //人均
                $arrHistoryInfo[$i]['perCapita'] = number_format($order->avgBusiness($arrHistoryInfo[$i]['businessId']),1);
                //月销值
                $arrHistoryInfo[$i]['monthNum'] = $allOrderNum;
            }
            return responseJson(Base::OK,'数据获取成功',$arrHistoryInfo);
        }catch (Exception $e){
            throw $e;
        }
    }

    //收藏店铺
    public function Favorites(Request $request,Favorites $favorites,Business $business,Order $order,CommodityAppraises $commodityAppraises){
        try{
            // TODO 将获取缓存信息放在中间件里
            //获取请求头
            $arr = $request->cacheInfo;
            //通过favorites表找出用户和商家Id
            if (($favoritesInfo = $favorites->findFavoritesInfo($arr['id']))->isEmpty()) {
                return responseJson(Base::OK,'暂无收藏店铺');
            }
            //将信息转为数组
            $arrFavoritesInfo = json_decode($favoritesInfo,true);
            //for循环数据
            for ($i=0;$i<count($arrFavoritesInfo);$i++){

                //通过businessId找到businessName,businessImg,deliverMoney,startDeliver
                $arrFavoritesInfo_businessInfo = $business->findFavoritesInfo($arrFavoritesInfo[$i]['businessId']);
                //将数据转为数组
                $arrBusinessInfo = json_decode($arrFavoritesInfo_businessInfo,true);
                //循环数据
                for ($j=0;$j<count($arrBusinessInfo);$j++){
                    $arrFavoritesInfo[$i]['businessName'] = $arrBusinessInfo[$j]['businessName'];
                    $arrFavoritesInfo[$i]['businessImg'] = $arrBusinessInfo[$j]['businessImg'];
                    //配送费
                    $arrFavoritesInfo[$i]['deliverMoney'] = $arrBusinessInfo[$j]['deliverMoney'];
                    //起送费
                    $arrFavoritesInfo[$i]['startDeliver'] = $arrBusinessInfo[$j]['startDeliver'];
                }

                //找出送外卖方式
                $arrFavoritesInfo[$i]['deliverType'] = $order->deliverType($arrFavoritesInfo[$i]['businessId']);

                //月销
                //获取月初年月日
                $beginDay = date('Y-m-01');
                //获取月末年月日
                $endDay = date('Y-m-d',strtotime("$beginDay +1 month -1 day"));
                //获取当前月份的销售额(月销值)
                $allOrderNum = $order->monthNum($beginDay,$endDay);

                //放入数组
                //五星评价
                $arrFavoritesInfo[$i]['Appraises'] = ceil(($commodityAppraises->commodityScore($arrFavoritesInfo[$i]['businessId']) + $commodityAppraises->serviceScore($arrFavoritesInfo[$i]['businessId']) + $commodityAppraises->timeScore($arrFavoritesInfo[$i]['businessId']))/3);
                //人均
                $arrFavoritesInfo[$i]['perCapita'] = number_format($order->avgBusiness($arrFavoritesInfo[$i]['businessId']),1);
                //月销值
                $arrFavoritesInfo[$i]['monthNum'] = $allOrderNum;
            }
            return responseJson(Base::OK,'数据获取成功',$arrFavoritesInfo);
        }catch (Exception $e){
            throw $e;
        }
    }
}